<template>
  <el-dialog
    v-loading="loading"
    :close-on-click-modal="false"
    :title="title"
    width="660px"
    :visible.sync="visible"
    @cancel="close"
  >
    <el-form ref="form" label-width="80px" :rules="rules" size="medium" :model="form">

      <el-form-item prop="name" label="名称">
        <el-input v-model="form.name" placeholder="请填入内容" />
      </el-form-item>

      <el-form-item prop="code" label="编码">
        <el-input v-model="form.code" placeholder="请填入内容" />
      </el-form-item>

      <el-form-item prop="description" label="说明">
        <el-input v-model="form.description" placeholder="请填入内容" />
      </el-form-item>
      <!-- <el-form-item prop="autoUpdate" label="自动更新">
        <el-switch
          v-model="form.autoUpdate"
          :active-value="true"
          :inactive-value="false"
        />
      </el-form-item> -->
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button class="cancel" @click="close">放弃</el-button>
      <el-button class="trueBtn" type="primary" :loading="loading" @click="handleSave">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addClassifyTpl, updateClassifyTpl, getClassifyTplDetail } from '@/api/wxappClassify'

export default {
  name: 'FormDialog',
  data() {
    return {
      loading: false,
      visible: false,
      title: '',
      mode: '',
      groupList: [],
      reject: null,
      resolve: null,
      permId: null,
      form: {
        name: null,
        description: null,
        remark: null,
        groupId: null,
        ranking: null
      },
      rules: {
        name: [{
          required: true,
          message: '请输入名称',
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '请输入编码',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    defaultForm() {
      return {
        name: null,
        type: 2,
        method: 'GET',
        uri: '',
        description: null,
        parentId: null,
        permList: []
      }
    },
    add(classifyCode) {
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
        this.visible = true
        this.mode = 'add'
        this.title = '添加分类模板'
        this.form = this.defaultForm()
        this.form.classifyCode = classifyCode
      })
    },

    edit(tplId) {
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
        this.visible = true
        this.mode = 'edit'
        this.title = '编辑分类模板'
        getClassifyTplDetail(tplId).then(res => {
          this.form = res.data
        })
      })
    },

    close() {
      this.visible = false
      this.resolve = null
      this.reject = null
      this.$refs.form.resetFields()
    },

    handleSave() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const data = this.form
          if (this.mode === 'add') {
            this.loading = true
            addClassifyTpl(data).then((resp) => {
              this.loading = false
              if (resp.code === 0) {
                this.$notify({ title: '成功', message: '添加成功', type: 'success', duration: 2000 })
                this.resolve && this.resolve()
                this.close()
              }
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.loading = true
            updateClassifyTpl(data).then((resp) => {
              this.loading = false
              if (resp.code === 0) {
                this.$notify({ title: '成功', message: '修改成功', type: 'success', duration: 2000 })
                this.resolve && this.resolve()
                this.close()
              }
            }).catch(() => {
              this.loading = false
            })
          }
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	::v-deep .el-dialog__header .el-dialog__title{
		color: $btnBgc;
	}
	::v-deep .el-input--medium .el-input__inner:focus{
		border-color: $btnBgc;
	}
	::v-deep.dialog-footer{
		.trueBtn{
			background-color: $btnBgc;
			border: none;
			color: white;
		}
		.trueBtn:hover{
			opacity: 0.5;
		}
		.cancel:hover{
			background-color: $btnBgc;
			border-color: white;
			color: white;
		}
	}
	::v-deep .el-switch.is-checked .el-switch__core{
		background-color: $btnBgc;
		border-color: white;
		color: white;
	}
	::v-deep .el-dialog__headerbtn .el-dialog__close:hover{
		color: $btnBgc;
	}
	 
	
</style>
